<?php
namespace app\common\model;

class Qds extends Base
{
    public static function search()
    {
        $page = input('post.page/d');
        $pageSize = input('post.pageSize/d');
        $page = ($page < 1) ? 1 : $page;
        $pageSize = ($pageSize < 1 || $pageSize > 50) ? 10 : $pageSize;
        $self = new static();
        $query = $self->alias('a');
        $query2 = clone $query;
        return [
            'total' => $query2->count('a.id'),
            'list' => $query->page($page, $pageSize)->field('nickname,day,qdtime,data,photo,date')->order('a.qdtime desc')->select()
        ];
    }
	
    public static function add($data)
    {
        $self = new static();
        $insert = [
            'uid' => $data['uid'],
            'nickname' => $data['nickname'],
			'photo' => $data['photo'],
            'data' => $data['data'],
            'day' => $data['day'],
            'qdtime' => date('y-m-d H:i:s'),
            'date' => date('Y-m-d H:i:s')
        ];
        if ($result = $self->insert($insert)) {
            return $result;
        }
    }

    public static function submit($userInfo, $qid)
    {
        $self = new static();
        if (!config('sys_qd_shuo_con') || !config('sys_qd_user_give_jf') || !config('sys_qd_day_gz') || !config('sys_sqd_give_type')) {
            $result = [
                'code' => 1,
                'message' => '站长还没配置好签到规则，暂时无法签到！'
            ];
        } else {
            if (false == Qqs::isQq($qid)) {
                $result = [
                    'code' => 1,
                    'message' => 'QQ号码不存在！'
                ];
            } elseif (false == Qqs::isQqZt($qid)) {
                $result = [
                    'code' => 2,
                    'message' => 'QQ状态已失效,无法进行签到',
                    'qq' => Qqs::findByqid($qid)['qq']
                ];
            } elseif (self::isTodayQds($userInfo['uid'])) {
                $result = [
                    'code' => 1,
                    'message' => '您今日已经签过到了，明天再来吧！'
                ];
            } else {
                $row_qq = Qqs::findByqid($qid);
                $run_url = get_domain() . 'Other/taskrun/app.php?act=shuo&qq=' . $row_qq['qq'] . '&skey=' . $row_qq['skey'] . '&pskey=' . $row_qq['pskey'] . '&content=' . config('sys_qd_shuo_con') . '&img=' . config('sys_qd_shuo_tu') . '&mode=1&runkey=' . md5(RUN_KEY) . '';
                send_get($run_url);
                $qd = self::findDayByUid($userInfo['uid']);
                $day = $qd['day'];
                $rule = self::qdRule();
                $day++;
                $max = isset($rule['max']) ? $rule['max'] : 0;
                if ($day > $max) {
                    $jf = $rule[$max];
                } else {
                    $jf = $rule[$day];
                }
                $count = $self
                    ->field('id')
                    ->where("TO_DAYS(date)=TO_DAYS(NOW())")
                    ->count();
                $day_gz = explode(",", config('sys_qd_user_give_jf'));
                $upJf = $day_gz[1] + $jf;
                if ($count == 0) {
                    $give = config('sys_sqd_give_type');
                    $shop_num = config('sys_sqd_shop_num');
                    switch ($give) {
                        case 1:
                            //开通时间
                            $vip_start = date("Y-m-d");
                            //计算开通的vip时长
                            if ($userInfo['viptime']) {
                                $vip_time = date("Y-m-d", strtotime("+" . $shop_num . " day", strtotime($userInfo['viptime'])));
                            } else {
                                $vip_time = date("Y-m-d", strtotime("+" . $shop_num . " day"));
                            }
                            Users::updateByUid($userInfo['uid'], [
                                'vipstart' => $vip_start,
                                'viptime' => $vip_time,
                                'jf' => $userInfo['jf'] + $upJf
                            ]);
                            self::add([
                                'uid' => $userInfo['uid'],
                                'nickname' => $userInfo['nickname'],
								'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
                                'data' => '连续签到' . $day . '天获得了' . $upJf . '个积分和VIP奖励！',
                                'day' => $day
                            ]);
                            $result = [
                                'code' => 0,
                                'message' => '恭喜您是今日第一个签到的用户连续签到' . $day . '天，奖励您' . $upJf . '积分和VIP会员' . $shop_num . '天！'
                            ];
                            break;
                        case 2:
                            Users::updateByUid($userInfo['uid'], [
                                'peie' => $userInfo['peie'] + $shop_num,
                                'jf' => $userInfo['jf'] + $upJf
                            ]);
                            self::add([
                                'uid' => $userInfo['uid'],
                                'nickname' => $userInfo['nickname'],
								'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
                                'data' => '连续签到' . $day . '天获得了' . $day_gz[1] . '积分和挂机配额奖励！',
                                'day' => $day
                            ]);
                            $result = [
                                'code' => 0,
                                'message' => '恭喜您是今日第一个签到的用户连续签到' . $day . '天，奖励您' . $upJf . '积分和挂机配额' . $shop_num . '个！'
                            ];
                            break;
                        case 3:
                            Users::updateByUid($userInfo['uid'], [
                                'money' => $userInfo['money'] + $shop_num,
                                'jf' => $userInfo['jf'] + $upJf
                            ]);
                            self::add([
                                'uid' => $userInfo['uid'],
								'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
                                'nickname' => $userInfo['nickname'],
                                'data' => '连续签到' . $day . '天获得了' . $day_gz[1] . '积分和账户余额' . $shop_num . '奖励！',
                                'day' => $day
                            ]);
                            $result = [
                                'code' => 0,
                                'message' => '恭喜您是今日第一个签到的用户连续签到' . $day . '天，奖励您' . $upJf . '积分和账户余额' . $shop_num . '元！'
                            ];
                            break;
                    }
                } elseif ($count <= $day_gz[1]) {
                    Users::updateByUid($userInfo['uid'], [
                        'jf' => $userInfo['jf'] + $upJf,
                    ]);
                    self::add([
                        'uid' => $userInfo['uid'],
                        'nickname' => $userInfo['nickname'],
						'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
                        'data' => '连续签到' . $day . '天获得了' . $day_gz[1] . '积分！',
                        'day' => $day
                    ]);
                    $result = [
                        'code' => 0,
                        'message' => '恭喜您是今日前' . $day_gz[0] . '名签到的用户连续签到' . $day . '天，奖励您' . $upJf . '积分！'
                    ];
                } else {
                    Users::updateByUid($userInfo['uid'], [
                        'jf' => $userInfo['jf'] + $day_gz[1],
                    ]);
                    self::add([
                        'uid' => $userInfo['uid'],
                        'nickname' => $userInfo['nickname'],
						'photo' => 'https://q1.qlogo.cn/g?b=qq&nk='.$userInfo['qq'].'&s=40&t=3046',
                        'data' => '连续签到' . $day . '天获得了' . $jf . '！',
                        'day' => $day
                    ]);
                    $result = [
                        'code' => 0,
                        'message' => '很遗憾您是今日第' . $count . '个签到的用户连续签到' . $day . '天，奖励您' . $jf . '积分！'
                    ];
                }
            }
        }
        return $result;
    }

    public static function qdRule()
    {
        if ($rows = explode(',', config('sys_qd_day_gz'))) {
            $array = array();
            foreach ($rows as $row) {
                if ($arr = explode(':', $row)) {
                    $key = trim($arr[0]);
                    $array["$key"] = trim($arr[1]);
                    $max = $key;
                }
            }

            if ($array) {
                $narr = array();
                $jf = 0;
                for ($i = 1; $i <= $max; $i++) {
                    if (isset($array[$i]) && $num = $array[$i]) {
                        $narr[$i] = $num;
                        $jf = $num;
                    } else {
                        $narr[$i] = $jf;
                    }
                }
                $narr['max'] = $max;
                return $narr;
            } else {
                return array(0, 1);
            }
        } else {
            return array(0, 1);
        }
    }

    public static function qdslist()
    {
        $self = new static();
        $result = $self
            ->order('qdtime desc')
            ->select();
        if ($result) {
            return $result;
        } else {
            return false;
        }
    }

    public static function isTodayQds($uid)
    {
        $self = new static();
        $isQd = $self->where(['uid' => $uid])->where('TO_DAYS(date)=TO_DAYS(NOW())')->find();
        if ($isQd) {
            return true;
        } else {
            return false;
        }
    }

    public static function findDayByUid($uid)
    {
        $self = new static();
        $result = $self
            ->where('uid', '=', $uid)
            ->order('date desc')
            ->find();
        return $result;
    }
}